﻿#include "FITKAbaModelInfo.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAssembly.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAssInstance.h"
#include "FITKAbaqusPart.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAbstractModelComponent.h"
#include "FITK_Interface/FITKInterfaceModel/FITKMeshSurface.h"
#include "FITK_Interface/FITKInterfaceModel/FITKModelSet.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaRefPoint.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKBoundaryLocation.h"
#include "FITK_Kernel/FITKCore/FITKDataRepo.h"

namespace AbaqusData
{
    AbaModelInfo AbaqusData::FITKAbaModelInfo::GetModelInfo(Interface::FITKAbstractModelComponent* comp)
    {
        AbaModelInfo info;
        //尝试强制类型转换，只能一种转换成功
        info._set = dynamic_cast<Interface::FITKModelSet*>(comp);
        info._surface = dynamic_cast<Interface::FITKMeshSurface*>(comp);
        info._refPoint = dynamic_cast<Interface::FITKAbaRefPoint*>(comp);
        //集合或者表面查找
        if (info._set)
            getSetSurfInfo(&info, info._set);
        else if (info._surface)
            getSetSurfInfo(&info, info._surface);
        else
            //参考点查找
            getRefPtInfo(&info, info._refPoint);

        return info;
    }

    AbaModelInfo AbaqusData::FITKAbaModelInfo::GetModelInfo(Interface::FITKBoundaryLocation* loc)
    {
         
        AbaModelInfo info; 
        auto componend = loc->getComponentObj();
        if (componend == nullptr) return info;
        //获取component原始信息
        info = GetModelInfo(componend);
        //清空关键位置信息
        info._assembly = nullptr;
        info._instance = nullptr;
        info._part = nullptr;
        Interface::FITKAbaAssembly* ass  = loc->getTModelObj<Interface::FITKAbaAssembly>();
        Interface::FITKAssemblyInstance* ins = loc->getTModelObj<Interface::FITKAssemblyInstance>();
        AbaqusData::FITKAbaqusPart* part = loc->getTModelObj<AbaqusData::FITKAbaqusPart>();

        if (ass)
            info._assembly = ass;
        else if (ins)
        {
            info._instance = ins;
            info._part = ins->getModelT<AbaqusData::FITKAbaqusPart>();
            // 查找装配体
            QList<Interface::FITKAbaAssembly* > assList = FITKDATAREPO->getData<Interface::FITKAbaAssembly>();
            for (Interface::FITKAbaAssembly* ass : assList)
            {
                if (!ass->isContains(ins)) continue;
                info._assembly = ass;
                break;
            }
        }
        else if (part)
            info._part = part;

        return info;
    }

    AbaModelInfo FITKAbaModelInfo::GetModelInfo(const int componentid)
    {
        AbaModelInfo abainfo;
        auto comp = FITKDATAREPO->getTDataByID<Interface::FITKAbstractModelComponent>(componentid);
        if (comp != nullptr)
            abainfo = GetModelInfo(comp);
        return abainfo;
    }

    AbaModelInfo FITKAbaModelInfo::GetModelInfo(const int componentid, int modelID)
    {
        AbaModelInfo info;
        info = GetModelInfo(componentid);
        //清空关键位置信息
        info._assembly = nullptr;
        info._instance = nullptr;
        info._part = nullptr;
        Interface::FITKAbaAssembly* ass = FITKDATAREPO->getTDataByID<Interface::FITKAbaAssembly>(modelID);
        Interface::FITKAssemblyInstance* ins = FITKDATAREPO->getTDataByID<Interface::FITKAssemblyInstance>(modelID);
        AbaqusData::FITKAbaqusPart* part = FITKDATAREPO->getTDataByID<AbaqusData::FITKAbaqusPart>(modelID);

        if (ass)
            info._assembly = ass;
        else if (ins)
        {
            info._instance = ins;
            info._part = ins->getModelT<AbaqusData::FITKAbaqusPart>();
            // 查找装配体
            QList<Interface::FITKAbaAssembly* > assList = FITKDATAREPO->getData<Interface::FITKAbaAssembly>();
            for (Interface::FITKAbaAssembly* ass : assList)
            {
                if (!ass->isContains(ins)) continue;
                info._assembly = ass;
                break;
            }
        }
        else if (part)
            info._part = part;


        return info;
    }

    void FITKAbaModelInfo::getSetSurfInfo(AbaModelInfo* info, Interface::FITKAbstractModelComponent* set)
    {
        if (info == nullptr || set == nullptr) return;
        //尝试进行类型转换
        info->_part = set->getAbstractModelAs<AbaqusData::FITKAbaqusPart>();
        info->_instance = set->getAbstractModelAs<Interface::FITKAssemblyInstance>();
        info->_assembly = set->getAbstractModelAs<Interface::FITKAbaAssembly>();

        //不是instance则结束
        if (info->_instance == nullptr) return;
        //根据instance查找part
        info->_part = info->_instance->getModelT<AbaqusData::FITKAbaqusPart>();
        //查找装配体
        QList<Interface::FITKAbaAssembly* > assList = FITKDATAREPO->getData<Interface::FITKAbaAssembly>();
        for (Interface::FITKAbaAssembly* ass : assList )
        {
            if (!ass->isContains(info->_instance)) continue;
            info->_assembly = ass;
            break;
        }
    }

    void FITKAbaModelInfo::getRefPtInfo(AbaModelInfo* info, Interface::FITKAbaRefPoint* surf)
    {
        //参考点只能在装配体
        info->_assembly = surf->getAbstractModelAs<Interface::FITKAbaAssembly>();
        //解析集合
        info->_set = surf->getSet();
    }

}




